/**
 * 基于WeUI的扩展支持
 */
import tnxvue, { build } from '../tnxvue.js';
import weui from 'weui.js';

import Button from './button/Button.vue';
import Cells from './cells/Cells.vue';
import EnumPicker from './enum-picker/EnumPicker.vue';
import FormItem from './form-item/FormItem.vue';
import FormItemError from './form-item-error/FormItemError.vue';
import FormItemGroup from './form-item-group/FormItemGroup.vue';
import Input from './input/Input.vue';
import Msg from './msg/Msg.vue';
import Picker from './picker/Picker.vue';
import SubmitForm from './submit-form/SubmitForm.vue';

export { build };

export default build('tnxvw', () => {
    window.weui = weui;

    const tnxvw = Object.assign({}, tnxvue, {
        libs: Object.assign({}, tnxvue.libs, {weui}),
        components: Object.assign({}, tnxvue.components, {
            Button,
            Cells,
            EnumPicker,
            FormItem,
            FormItemError,
            FormItemGroup,
            Input,
            Msg,
            Picker,
            SubmitForm,
        }),
        icons: {
            success: 'weui-icon-success',
            info: 'weui-icon-info text-info',
            warning: 'weui-icon-warn text-warning',
            error: 'weui-icon-warn',
        },
        alertInstance: null,
        _closeMessage() {
            if (this.alertInstance) {
                this.alertInstance.hide();
                this.alertInstance = null;
            }
            this.hideLoading();
        },
        alert(message, title, options) {
            this._closeMessage();

            if (typeof title === 'object') {
                options = title;
                title = '提示';
            }
            return new Promise((resolve) => {
                let iconClass;
                if (options && options.type) {
                    iconClass = this.icons[options.type];
                    delete options.type;
                }
                iconClass = iconClass || this.icons.info;
                let content = '<div class="d-flex">'
                    + '<div><i class="' + iconClass + '"></i></div>'
                    + '<div style="margin-left: 4px; word-break: break-all;">' + message + '</div>'
                    + '</div>';
                this.alertInstance = weui.alert(content, resolve, Object.assign({}, options, {
                    title: title,
                }));
            });
        },
        success(message, options) {
            return this.alert(message, '成功', Object.assign({}, options, {
                type: 'success'
            }));
        },
        error(message, options) {
            return this.alert(message, '错误', Object.assign({}, options, {
                type: 'error'
            }));
        },
        toast(message, timeout, options) {
            this._closeMessage();

            if (typeof timeout === 'object') {
                options = timeout;
                timeout = undefined;
            }
            return new Promise((resolve) => {
                options = Object.assign({}, options, {
                    duration: timeout,
                    callback: resolve,
                });
                weui.toast(message, options);
            });
        },
        loadingInstance: null,
        showLoading(message, options) {
            this._closeMessage();

            this.loadingInstance = weui.loading(message, options);
        },
        hideLoading() {
            return new Promise((resolve) => {
                if (this.loadingInstance) { // 确保绝对的单例
                    this.loadingInstance.hide(resolve);
                    this.loadingInstance = null;
                } else {
                    resolve();
                }
            });
        },
        closeLoading() {
            return this.hideLoading();
        },
    });

    tnxvw.libs.Vue.use(tnxvw);

    tnxvw.app.rpc.handleOtherError = function(message) {
        window.tnx._closeMessage();
        weui.topTips(message);
        console.error(message);
    }
    return tnxvw;
});
